MODULE menu_plot
  !-----------------------------------------------------------
  ! KRUUNUNHAKA basis set tool kit - completeness profile menu
  !-----------------------------------------------------------
  ! See CHANGELOG
  !-----------------------------------------------------------
  ! Copyright (C) 2003-2008 Pekka Manninen, 2010 Jussi Lehtola
  !
  ! This program is distributed under the terms of the GNU General
  ! Public License as published by the Free Software Foundation;
  ! either version 2 of the License, or (at your option) any later version.
  !
  ! This program is distributed in the hope that it will be useful,
  ! but WITHOUT ANY WARRANTY; without even the implied warranty of
  ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  !
  ! Please note that this program as well as its 
  ! author must be properly cited whenever the program or some parts 
  ! originated on it are employed. 
  !
  !-----------------------------------------------------------

  USE definitions
  USE textutil
  USE menu
  USE completeness_profile

  IMPLICIT NONE

CONTAINS

  SUBROUTINE plot_menu(X_l,C_l,nbas)
    ! Completeness profile plotting menu 
    IMPLICIT NONE
    REAL(KIND=prec), DIMENSION(:,:), ALLOCATABLE, INTENT(IN) :: X_l
    REAL(KIND=prec), DIMENSION(:,:,:), ALLOCATABLE, INTENT(IN) :: C_l
    INTEGER, DIMENSION(MAXL,2), INTENT(IN) :: nbas

    CHARACTER(LEN=TERMWIDTH) :: plotmenu_tit
    CHARACTER(LEN=TERMWIDTH), DIMENSION(6) :: plotmenu
    CHARACTER(LEN=TERMWIDTH), DIMENSION(5) :: currentvals
    CHARACTER(LEN=6) :: plotsel

    CHARACTER(LEN=TERMWIDTH), SAVE :: filename='profile.dat'

    ! Scanning range
    REAL(KIND=prec), SAVE :: low=1e-10_prec
    REAL(KIND=prec), SAVE :: high=1e10_prec
    ! Amount of points in scanning range
    INTEGER, SAVE :: npoints=2000
    ! Use an STO to scan?
    LOGICAL, SAVE :: usesto=.FALSE.

    ! Exponents
    REAL(KIND=prec), DIMENSION(:), ALLOCATABLE :: alphas
    ! Completeness profile
    REAL(KIND=prec), DIMENSION(:,:), ALLOCATABLE :: Y

    ! Loop indices
    INTEGER :: ia, l

    INTEGER :: sel

    LOGICAL :: ok

    plotmenu_tit="Completeness profile menu"
    plotmenu(1)='Set file name'
    plotmenu(2)='Set limits of scanning range'
    plotmenu(3)='Change number of points'
    plotmenu(4)='Change scanning mode'
    plotmenu(5)='Perform scan'
    plotmenu(6)='Return to main menu'
    plotsel='12345r'

    ok=.FALSE.

    DO WHILE(.NOT. ok)
       WRITE(currentvals(1),'(A,A)') 'Filename is ',TRIM(filename)
       IF(usesto) THEN
          WRITE(currentvals(2),'(A)') 'Scanning with an STO'
       ELSE
          WRITE(currentvals(2),'(A)') 'Scanning with a GTO'
       END IF
       WRITE(currentvals(3),'(A,E8.2)') 'Lower limit of scanning range is ',low
       WRITE(currentvals(4),'(A,E8.2)') 'Upper limit of scanning range is ',high
       WRITE(currentvals(5),'(A,I5,A)') 'Using ',npoints,' points in plot'
       
       sel=make_cvals_selection(plotmenu_tit,currentvals,plotmenu,plotsel,'*')
       
       SELECT CASE(sel)
       CASE(1)
          ! Set input file name
          WRITE (*,'(A)',advance='no') 'Give input file name: '
          READ (*,*) filename
          WRITE (*,*) ''

       CASE(2)
          ! Set limits
          low=1.0_prec
          high=0.0_prec
          DO WHILE(low>high .OR. low<0)
             CALL read_limits('Give limits of scanning range:',low,high);
             IF(low>high .OR. low<0) THEN
                WRITE (*,'(A)') 'Invalid input.'
             END IF
          END DO

       CASE(3)
          ! Set number of points
          npoints=-1
          DO WHILE(npoints<=0)
             npoints=read_int('Give number of points:')
             IF(npoints<=0) THEN
                WRITE (*,'(A)') 'Invalid input.'
             END IF
          END DO

       CASE(4)
          usesto=.NOT. usesto

       CASE(5)
          ! Allocate scanning exponent table
          ALLOCATE(alphas(npoints))
          ! Allocate completeness profile array
          ALLOCATE(Y(npoints,MAXL))
          
          ! Get alphas
          CALL get_alphas(low,high,alphas)

          ! Initialize Y
          Y=0.0_prec
          ! Calculate profiles
          DO l=1,MAXL
             ! Compute profiles
             IF(nbas(l,1)>0) THEN
                ! Allocate table for normalized contraction coefficients
!                ALLOCATE(normC(nbas(l,1),nbas(l,2)))
                ! Normalize contractions
!                CALL normalize_contraction(l,X_l(l,1:nbas(l,1)),C_l(l,1:nbas(l,1),1:nbas(l,2)),normC)
                ! Compute profile
!                CALL comp_prof(l, X_l(l,1:nbas(l,1)), normC, alphas, usesto, Y(:,l))
                ! Deallocate helper
!                DEALLOCATE(normC)

                CALL comp_prof(l, X_l(l,1:nbas(l,1)), C_l(l,1:nbas(l,1),1:nbas(l,2)), alphas, usesto, Y(:,l))
             END IF
          END DO

          ! Open output file
          OPEN (20, FILE = filename, STATUS='replace')
          ! Write completenesses
          call print_centerbar('Completeness of space','-')
          DO l=1,MAXL
             WRITE(*,'(A6,A1,A3)',advance='no') ' ',l_name(l),' '
          END DO
          WRITE(*,*)

          DO l=1,MAXL
             IF(nbas(l,1)>0) THEN
                WRITE(*,'(F10.5)',advance='no') SUM(Y(:,l)) / npoints
                WRITE(20,'(A,A,A,F7.5)') '# ', l_name(l), '-space completeness ', SUM(Y(:,l)) / npoints
             ELSE
                WRITE(*,'(F10.5)',advance='no') 0.0
                WRITE(20,'(A,A,A,F7.5)') '# ', l_name(l), '-space completeness ', 0.0
             END IF
          END DO
          WRITE(*,*) ''
          WRITE(*,*) ''

          ! Write profiles
          DO ia = 1, SIZE(alphas)
             WRITE (20,'(E24.17,7E24.17)') LOG10(alphas(ia)), Y(ia,:)
          END DO
          
          ! Close output
          CLOSE (20)

          ! Deallocate memory
          DEALLOCATE(alphas)
          DEALLOCATE(Y)
          
          ok=.TRUE.

       CASE(6)
          ok=.TRUE.          
       END SELECT
    END DO
  END SUBROUTINE plot_menu

END MODULE MENU_PLOT
